package com.ruoyi.web.controller.bussiness;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PatrolAreaUserBo;
import com.ruoyi.system.domain.vo.PatrolAreaUserVo;
import com.ruoyi.system.service.IPatrolAreaUserService;
import com.ruoyi.web.controller.system.BatchAssignRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 巡区用户分配
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/patrolAreaUser")
public class PatrolAreaUserController extends BaseController {

    private final IPatrolAreaUserService patrolAreaUserService;

    /**
     * 查询巡区用户分配列表
     */
    @SaCheckPermission("system:patrolAreaUser:list")
    @GetMapping("/list")
    public TableDataInfo<PatrolAreaUserVo> list(PatrolAreaUserBo bo, PageQuery pageQuery) {
        return patrolAreaUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡区用户分配列表
     */
    @SaCheckPermission("system:patrolAreaUser:export")
    @Log(title = "巡区用户分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatrolAreaUserBo bo, HttpServletResponse response) {
        List<PatrolAreaUserVo> list = patrolAreaUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡区用户分配", PatrolAreaUserVo.class, response);
    }

    /**
     * 获取巡区用户分配详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:patrolAreaUser:query")
    @GetMapping("/{id}")
    public R<PatrolAreaUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(patrolAreaUserService.queryById(id));
    }

    /**
     * 根据巡区ID查询用户分配列表
     */
    @SaCheckPermission("system:patrolAreaUser:query")
    @GetMapping("/area/{areaId}")
    public R<List<PatrolAreaUserVo>> getUsersByAreaId(@NotNull(message = "巡区ID不能为空")
                                                     @PathVariable Long areaId) {
        return R.ok(patrolAreaUserService.queryByAreaId(areaId));
    }

    /**
     * 根据用户ID查询巡区分配列表
     */
    @SaCheckPermission("system:patrolAreaUser:query")
    @GetMapping("/user/{userId}")
    public R<List<PatrolAreaUserVo>> getAreasByUserId(@NotNull(message = "用户ID不能为空")
                                                     @PathVariable Long userId) {
        return R.ok(patrolAreaUserService.queryByUserId(userId));
    }

    /**
     * 新增巡区用户分配
     */
    @SaCheckPermission("system:patrolAreaUser:add")
    @Log(title = "巡区用户分配", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatrolAreaUserBo bo) {
        return toAjax(patrolAreaUserService.insertByBo(bo));
    }

    /**
     * 修改巡区用户分配
     */
    @SaCheckPermission("system:patrolAreaUser:edit")
    @Log(title = "巡区用户分配", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolAreaUserBo bo) {
        return toAjax(patrolAreaUserService.updateByBo(bo));
    }

    /**
     * 删除巡区用户分配
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:patrolAreaUser:remove")
    @Log(title = "巡区用户分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                         @PathVariable Long[] ids) {
        return toAjax(patrolAreaUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 批量分配用户到巡区
     */
    @SaCheckPermission("system:patrolAreaUser:assign")
    @Log(title = "批量分配用户到巡区", businessType = BusinessType.INSERT)
    @PostMapping("/batchAssign")
    public R<Void> batchAssignUsers(@RequestBody BatchAssignRequest request) {
        return toAjax(patrolAreaUserService.batchAssignUsers(request.getAreaId(), request.getUserIds(), request.getUserNames(), request.getAssignType()));
    }

    /**
     * 取消用户巡区分配
     */
    @SaCheckPermission("system:patrolAreaUser:remove")
    @Log(title = "取消用户巡区分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/cancel")
    public R<Void> cancelUserAssign(@RequestParam Long areaId,
                                   @RequestParam Long userId) {
        return toAjax(patrolAreaUserService.cancelUserAssign(areaId, userId));
    }
}
